package demo.vo.common;

import com.alibaba.fastjson.JSON;

import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * QPS 同比环比信息
 *
 * @author junying.han
 */
public class QpsRatioInfo {

    public static final String HB_VALUE = "hbValue";
    /**
     * 保留两位小数点的百分比格式
     */
    public static final NumberFormat PERCENT_FORMAT = NumberFormat.getPercentInstance();

    static {
        PERCENT_FORMAT.setMaximumFractionDigits(2);
    }

    /**
     * 当前值
     */
    private long value;
    /**
     * 环比值
     */
    private long hbValue;
    /**
     * 同比值
     */
    private long tbValue;

    public QpsRatioInfo() {
        super();
    }

    public QpsRatioInfo(long value, long hbValue, long tbValue) {
        super();
        this.value = value;
        this.hbValue = hbValue;
        this.tbValue = tbValue;
    }

    public static String percentRatio(double ratio) {
        if (Double.compare(Double.NaN, ratio) == 0) {
            return "";
        }
        return PERCENT_FORMAT.format(ratio);
    }

    public static void main(String[] args) {
        QpsRatioInfo qps = new QpsRatioInfo();
        qps.setValue(750);
        qps.setHbValue(1260);
        qps.setTbValue(1200);
        System.out.println(qps.toMap());
        System.out.println(qps.desc());
    }

    public QpsRatioInfo copy() {
        return new QpsRatioInfo(value, hbValue, tbValue);
    }

    public long getHbValue() {
        return hbValue;
    }

    public void setHbValue(long hbValue) {
        this.hbValue = hbValue;
    }

    public long getTbValue() {
        return tbValue;
    }

    public void setTbValue(long tbValue) {
        this.tbValue = tbValue;
    }

    public long getValue() {
        return value;
    }

    public void setValue(long value) {
        this.value = value;
    }

    /**
     * 计算环比之差
     *
     * @return
     */
    public long hbDiff() {
        return value - hbValue;
    }

    /**
     * 计算环比之差
     *
     * @return
     */
    public double hbRatio() {
        if (hbValue <= 0L) {
            return Double.NaN;
        }
        return (double) hbDiff() / hbValue;
    }

    /**
     * 计算同比之差
     *
     * @return
     */
    public long tbDiff() {
        return value - tbValue;
    }

    /**
     * 计算同比之差
     *
     * @return
     */
    public double tbRatio() {
        if (tbValue <= 0L) {
            return Double.NaN;
        }
        return (double) tbDiff() / tbValue;
    }

    public Map<String, Object> toMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("value", value);

        map.put(HB_VALUE, hbValue);
        map.put("hbDiff", hbDiff());
        map.put("hbRatio", hbRatio());

        map.put("tbValue", tbValue);
        map.put("tbDiff", tbDiff());
        map.put("tbRatio", tbRatio());
        return map;
    }

    public String desc() {
        String header = "";
        long diff = hbDiff();
        if (diff > 0) {
            header = "流量上涨";
        } else if (diff < 0) {
            header = "流量下跌";
        } else {
            header = "流量持平";
        }
        if (value < 1) {
            header = "流量跌零";
        }
        return header + "：" + String.format("环比变化 %s（%s -> %s）%s，日同比变化 %s（%s -> %s）%s",
                hbDiff(), hbValue, value, percentRatio(hbRatio()),
                tbDiff(), tbValue, value, percentRatio(tbRatio()));
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

}
